// import http from "@/utils/http";
import { message } from 'ant-design-vue';
	
export default {
  data() {
    return {
			items: [] as object[],
			loading: false as boolean,
			loadstate: 0 as number,
			pagination: {
			  total: 0 as number,
			  current: 1 as number,
			  pageSize: 10 as number,
			},
			searchData: {
				name: '' as string
			},
			editData: {},
      editField: ''
		}
  },
	methods: {
		fetch(params: object){
			this.loadstate = 1
			this.page = 1
			this.items = []
			this.loading = true
			return useGet(this.loadPath, params).then((data: object) => {
				this.loadData(data)
				this.loadstate = 2
			}).finally(()=>{
				this.loading = false
			}).catch(()=>{})
		},
		loadData(data: {items: object[], total: number}){
			this.items = data.items
			this.pagination.total = data.total
		},
		tableChange(
		  pag: { pageSize: number; current: number },
		  filters: any,
		  sorter: any,
		) {
			Object.assign(this.pagination, pag)
		  this.fetch({...this.searchData, page: this.pagination.current});
		},
		updated(record: {id: number}){
			if(record && record.id){
				const item = this.items.find((r: {id: number})=>r.id==record.id);
				if(item){		
					record && Object.assign(item, record)
				} else {
					this.items.unshift(record);
				}
			}
		},
		del(id: number) {
			useDelete(this.loadPath, {id: id}, {loading: true}).then(({id: number}) => {
				this.items = this.items.filter((r: object)=>r.id != id)
				message.success('删除成功')
			}).catch(()=>{})
		},
		put(record: {id: number}){
			usePut(this.loadPath, record).then((data: any) => {
				this.updated(data)
			}).catch(()=>{})
		},
		editCell(record: {id: number}){
			this.put(record)
			for(const i in this.editData){
        this.editData[i] = 0
      }
		}
	}
}
